package edu.uci.ics.ai.maze.views;

import java.awt.Component;
import java.awt.Dimension;
import java.awt.Frame;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.SwingConstants;

/**
 * View defining the About dialog.
 */
public class AboutDialog extends JDialog {

  private final JButton closeButton;
  private final JLabel aboutLabel;
  // Used for addNotify check.
  private boolean fComponentsAdjusted = false;

  public AboutDialog(Frame parentFrame) {
    super(parentFrame, "About Game");
    setModal(true);
    getContentPane().setLayout(new GridBagLayout());
    setSize(248, 94);
    setVisible(false);
    CloseWindowListener listener = new CloseWindowListener();
    addWindowListener(listener);

    closeButton = new JButton("Close");
    closeButton.setActionCommand("Close");
    closeButton.setOpaque(false);
    closeButton.setMnemonic((int) 'C');
    getContentPane().add(closeButton,
            new GridBagConstraints(2, 1, 1, 1, 0.0, 0.0,
            GridBagConstraints.CENTER, GridBagConstraints.NONE,
            new Insets(0, 0, 10, 0), 0, 0));
    closeButton.setBounds(98, 59, 51, 25);
    closeButton.addActionListener(listener);

    aboutLabel = new JLabel("Game Version 2010");
    aboutLabel.setHorizontalAlignment(SwingConstants.CENTER);
    getContentPane().add(aboutLabel,
            new GridBagConstraints(0, 0, 3, 1, 1.0, 1.0,
            GridBagConstraints.CENTER, GridBagConstraints.BOTH,
            new Insets(0, 0, 0, 0), 0, 0));
    aboutLabel.setBounds(0, 0, 248, 59);
  }

  @Override
  public void setVisible(boolean visible) {
    if (visible) {
      Rectangle bounds = getParent().getBounds();
      Dimension size = getSize();
      setLocation(bounds.x + (bounds.width - size.width) / 2,
              bounds.y + (bounds.height - size.height) / 2);
    }

    super.setVisible(visible);
  }

  @Override
  public void addNotify() {
    // Record the size of the window prior to calling parents addNotify.
    Dimension d = getSize();
    super.addNotify();
    if (fComponentsAdjusted) {
      return;
    }
    // Adjust components according to the insets
    Insets insets = getInsets();
    setSize(insets.left + insets.right + d.width, insets.top + insets.bottom + d.height);
    Component components[] = getContentPane().getComponents();
    for (int i = 0; i < components.length; i++) {
      Point p = components[i].getLocation();
      p.translate(insets.left, insets.top);
      components[i].setLocation(p);
    }
    fComponentsAdjusted = true;
  }

  class CloseWindowListener extends WindowAdapter implements ActionListener {

    @Override
    public void windowClosing(WindowEvent event) {
      setVisible(false);
    }

    @Override
    public void actionPerformed(ActionEvent event) {
      setVisible(false);
    }
  }

}
